mobileでURLを外部リンク記法に変換するPage menu
deprecated
code:mod.js
var N=e=>typeof e=="object"&&e!==null,R=e=>N(e)?(e.name===void 0||typeof e.name=="string")&&typeof e.message=="string":!1,m=e=>{try{let t=typeof e=="string"?JSON.parse(e):e;return R(t)?t:!1}catch(t){if(t instanceof SyntaxError)return!1;throw t}};var E=(e,t)=>{if(!(e instanceof HTMLDivElement))throw new TypeError("${t}" must be HTMLDivElememt but actual is "${e}")};var w=()=>H(document.getElementsByClassName("status-bar")?.0,"div.status-bar"),H=(e,t)=>{if(!!e)return E(e,t),e};function b(){let e=w();if(!e)throw new Error("div.status-bar can't be found");let t=document.createElement("div");return e.append(t),{render:(...n)=>{t.textContent="";let r=T(...n);r&&t.append(r)},dispose:()=>t.remove()}}function T(...e){let t=e.flatMap(r=>{switch(r.type){case"spinner":returnP();case"check-circle":returnU();case"exclamation-triangle":returnA();case"text":returnx(r.text);case"group":{let s=T(...r.items);return s?s:[]}}});if(t.length===0)return;if(t.length===1)return t0;let n=document.createElement("span");return n.classList.add("item-group"),n.append(...t),n}function x(e){let t=document.createElement("span");return t.classList.add("item"),t.append(e),t}function P(){let e=document.createElement("i");return e.classList.add("fa","fa-spinner"),x(e)}function U(){let e=document.createElement("i");return e.classList.add("kamon","kamon-check-circle"),x(e)}function A(){let e=document.createElement("i");return e.classList.add("fas","fa-exclamation-triangle"),x(e)}var u=e=>{let{fetch:t=globalThis.fetch,hostName:n="scrapbox.io",...r}=e;return{fetch:t,hostName:n,...r}};var a=class extends Error{constructor(t){super(${t.status} ${t.statusText} when fetching ${t.path.toString()});this.name="UnexpectedResponseError";this.status=t.status,this.statusText=t.statusText,this.body=t.body,this.path=t.path,Error.captureStackTrace&&Error.captureStackTrace(this,a)}};var k=async e=>{let{sid:t,hostName:n,fetch:r}=u(e??{}),s=https://${n}/api/users/me,i=await r(s,t?{headers:{Cookie:p(t)}}:void 0);if(!i.ok)throw new a({path:new URL(s),...i,body:await i.text()});return await i.json()};var p=e=>connect.sid=${e},d=async e=>window._csrf?window._csrf:(await k(e)).csrfToken;var M=async(e,t)=>{let{sid:n,hostName:r,fetch:s,csrf:i}=u(t??{}),c=https://${r}/api/embed-text/url?url=${encodeURIComponent(e.toString())},o=await s(c,{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8","X-CSRF-TOKEN":i??await d(t),...n?{Cookie:p(n)}:{}},body:JSON.stringify({timeout:3e3})});if(!o.ok){if(o.status===422)return{ok:!1,value:{name:"InvalidURLError",message:(await o.json()).message}};let l=await o.json(),g=m(l);if(!g)throw new a({path:new URL(c),...o,body:await o.text()});return{ok:!1,value:g}}let{title:f}=await o.json();return{ok:!0,value:f}};var v=async(e,t)=>{let{sid:n,hostName:r,fetch:s,csrf:i}=u(t??{}),c=https://${r}/api/embed-text/twitter?url=${encodeURIComponent(e.toString())},o=await s(c,{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8","X-CSRF-TOKEN":i??await d(t),...n?{Cookie:p(n)}:{}},body:JSON.stringify({timeout:3e3})});if(!o.ok){if(o.status===422)return{ok:!1,value:{name:"InvalidURLError",message:(await o.json()).message}};let l=await o.json(),g=m(l);if(!g)throw new a({path:new URL(c),...o,body:await o.text()});return{ok:!1,value:g}}let f=await o.json();return{ok:!0,value:f}};var On={title:"convert URL",onClick:e=>$(e)};async function $(e){let{render:t,dispose:n}=b();t({type:"spinner"},{type:"text",text:"convert URLs..."});let r=0;try{let s=await Promise.all(e.split(/(https?:\/\/\S+)/).map(async i=>{if(!/^https?:\/\/\S+/.test(i))return i;let c;try{c=new URL(i)}catch(l){if(l instanceof TypeError)return i;throw l}if(K(c)){let l=await v(c);return l.ok?(r++,F(c,l.value)):i}let o=await M(c);if(!o.ok)return i;r++;let f=o.value.replace(/\s/g," ").replaceAll("","[").replaceAll("","]");return[${c} ${f}]}));return t({type:"check-circle"},{type:"text",text:Converted ${r} URLs.}),s.join("")}catch(s){t({type:"exclamation-triangle"},{type:"text",text:s instanceof Error?${s.name} ${s.message}:"Unknown error! (see developper console)"}),console.error(s)}finally{setTimeout(()=>n(),1e3)}}var K=e=>/^https:\/\/(?:www\.|mobile\.|m\.|)twitter\.com\/(A-Za-z0-9_*)\/(?:status|statuses)\/\d+/.test(e.toString()),h=e=>e.replace(/\b/gm,"").replace(/\s\r\n\u2028\u2029+/gm," ").replace(/\s*[\]\s*/g," ").trim(),F=(e,t)=>[${h(t.screenName)}(@${h(t.userName)}) ${e.origin}${e.pathname},...t.description.split( ).map(n=>> ${h(n)}),...t.images.length>0?[> ${t.images.map(n=>[${n}])}]:[]].join(
`);export{On as urlConverter};